package ex05.pyrmont.startup;

import ex05.pyrmont.core.SimpleContextV5;
import ex05.pyrmont.core.SimpleContextMapperV5;
import ex05.pyrmont.core.SimpleLoaderV5;
import ex05.pyrmont.core.SimpleWrapperV5;
import ex05.pyrmont.valves.ClientIPLoggerValveV5;
import ex05.pyrmont.valves.HeaderLoggerValveV5;
import org.apache.catalina.Context;
import org.apache.catalina.Loader;
import org.apache.catalina.Mapper;
import org.apache.catalina.Pipeline;
import org.apache.catalina.Valve;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.http.HttpConnector;

public final class BootstrapV5_2 {


    public static void main(String[] args) {






        HttpConnector connector = new HttpConnector();
        Wrapper wrapper1 = new SimpleWrapperV5();
        wrapper1.setName("PrimitiveServlet");
        wrapper1.setServletClass("PrimitiveServlet");
        Wrapper wrapper2 = new SimpleWrapperV5();
        wrapper2.setName("ModernServlet");
        wrapper2.setServletClass("ModernServlet");

        Context context = new SimpleContextV5();
        context.addChild(wrapper1);
        context.addChild(wrapper2);

        Valve valve1 = new HeaderLoggerValveV5();
        Valve valve2 = new ClientIPLoggerValveV5();

        ((Pipeline) context).addValve(valve1);
        ((Pipeline) context).addValve(valve2);

        Mapper mapper = new SimpleContextMapperV5();
        mapper.setProtocol("http");
        context.addMapper(mapper);
        Loader loader = new SimpleLoaderV5();
        context.setLoader(loader);
        // context.addServletMapping(pattern, name);
        context.addServletMapping("/servlet/PrimitiveServlet", "PrimitiveServlet");
        context.addServletMapping("/servlet/ModernServlet", "ModernServlet");
        connector.setContainer(context);
        connector.setDebug(4);
        try {
            connector.initialize();
            connector.start();
            System.in.read();   // make the application wait until we press a key.
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
